<?php
/**
 * Created by PhpStorm.
 * User: hantian
 * Date: 2017/9/6
 * Time: 下午3:01
 */

namespace App\Services;

use Payment\Common\PayException;
use Payment\Client\Charge;
use Payment\Config;

final class PaymentService
{
    /**
     * @param $payType
     * const ALI_CHANNEL_APP = 'ali_app';// 支付宝 手机app 支付
     * const ALI_CHANNEL_WAP = 'ali_wap';// 支付宝 手机网页 支付
     * const ALI_CHANNEL_WEB = 'ali_web';// 支付宝 PC 网页支付
     * const ALI_CHANNEL_QR = 'ali_qr';// 支付宝 扫码支付
     * const ALI_CHANNEL_BAR = 'ali_bar';// 支付宝 条码支付
     * ===============
     * const WX_CHANNEL_APP = 'wx_app';// 微信 APP 支付
     * const WX_CHANNEL_PUB = 'wx_pub';// 微信 公众账号 支付
     * const WX_CHANNEL_QR = 'wx_qr';// 微信 扫码支付  (可以使用app的帐号，也可以用公众的帐号完成)
     * const WX_CHANNEL_BAR = 'wx_bar';// 微信 刷卡支付，与支付宝的条码支付对应
     * const WX_CHANNEL_LITE = 'wx_lite';// 微信小程序支付
     * const WX_CHANNEL_WAP = 'wx_wap';// 微信wap支付，针对特定用户
     * ===============
     * const CMB_CHANNEL_APP = 'cmb_app';// 招商 app  ，实际上招商并无该概念
     * const CMB_CHANNEL_WAP = 'cmb_wap';// 招商h5支付，其实app支付也是使用的h5
     * const CMB_BIND = 'cmb_bind';// 签约API
     * const CMB_PUB_KEY = 'cmb_pub_key';// 查询招商公钥
     * const CMB_CHARGE = 'cmb_charge';// 招商支付
     * const CMB_REFUND = 'cmb_refund';// 招商退款
     *
     * @param $payWay
     * const WECHAT_PAY = 'wechat';
     * const ALI_PAY = 'ali';
     * const CMB_PAY = 'cmb';
     *
     * @param $payData
     *
     * @return object
     */
    public static function pay($payType, $payWay, $payData)
    {
        $config = self::getConfig($payWay);
        try {
            $ret = Charge::run($payType, $config, $payData);
            return $ret;
        } catch (PayException $e) {
            echo $e->errorMessage();
            return $e;
        }

    }

    public static function getConfig($payWay)
    {
        return \Illuminate\Support\Facades\Config::get('payment.' . $payWay);
    }
}